#include<iostream>
#include"UdpServer.hpp"

void Usage(std::string proc)
{
    std::cerr << "Usage: " << proc << " serverip serverport" << std::endl;
}

int main(int argc,char* argv[])
{
    if(argc!=3)
    {
        Usage(argv[0]);
        exit(0);
    }

    std::string serverip = argv[1];
    uint16_t serverport=std::stoi(argv[2]);

    int sockid=socket(AF_INET,SOCK_DGRAM,0);
    if(sockid<0)
    {
        std::cerr << "create socket errror" << std::endl;
        return 0;
    }

    struct sockaddr_in server;
    memset(&server, 0, sizeof(server));
    server.sin_family = AF_INET;
    server.sin_port = htons(serverport);
    server.sin_addr.s_addr = inet_addr(serverip.c_str());

    while(true)
    {
        std::cout<<"Please Enter@ ";
        std::string in;
        std::getline(std::cin,in);
        sendto(sockid,in.c_str(),in.size(),0,(struct sockaddr*)&server,sizeof(server));
        
        struct sockaddr_in temp;
        socklen_t len=sizeof(temp);
        char buffer[1024];
        int m = recvfrom(sockid,buffer,sizeof(buffer),
            0,(struct sockaddr*)&temp,&len);
        if(m > 0)
        {
            buffer[m]=0;
            std::cout<<buffer<<std::endl;
        }
    }
    return 0;
}